/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gianluca.sagrafx.entities;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.gianluca.sagrafx.entities.exceptions.NonexistentEntityException;

/**
 *
 * @author eginfer
 */
public class OrdiniJpaController implements Serializable {

    public OrdiniJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Ordini ordini) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pietanze idPietanza = ordini.getIdPietanza();
            if (idPietanza != null) {
                idPietanza = em.getReference(idPietanza.getClass(), idPietanza.getId());
                ordini.setIdPietanza(idPietanza);
            }
            em.persist(ordini);
            if (idPietanza != null) {
                idPietanza.getOrdiniCollection().add(ordini);
                idPietanza = em.merge(idPietanza);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Ordini ordini) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ordini persistentOrdini = em.find(Ordini.class, ordini.getId());
            Pietanze idPietanzaOld = persistentOrdini.getIdPietanza();
            Pietanze idPietanzaNew = ordini.getIdPietanza();
            if (idPietanzaNew != null) {
                idPietanzaNew = em.getReference(idPietanzaNew.getClass(), idPietanzaNew.getId());
                ordini.setIdPietanza(idPietanzaNew);
            }
            ordini = em.merge(ordini);
            if (idPietanzaOld != null && !idPietanzaOld.equals(idPietanzaNew)) {
                idPietanzaOld.getOrdiniCollection().remove(ordini);
                idPietanzaOld = em.merge(idPietanzaOld);
            }
            if (idPietanzaNew != null && !idPietanzaNew.equals(idPietanzaOld)) {
                idPietanzaNew.getOrdiniCollection().add(ordini);
                idPietanzaNew = em.merge(idPietanzaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = ordini.getId();
                if (findOrdini(id) == null) {
                    throw new NonexistentEntityException("The ordini with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ordini ordini;
            try {
                ordini = em.getReference(Ordini.class, id);
                ordini.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ordini with id " + id + " no longer exists.", enfe);
            }
            Pietanze idPietanza = ordini.getIdPietanza();
            if (idPietanza != null) {
                idPietanza.getOrdiniCollection().remove(ordini);
                idPietanza = em.merge(idPietanza);
            }
            em.remove(ordini);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ordini> findOrdiniEntities() {
        return findOrdiniEntities(true, -1, -1);
    }

    public List<Ordini> findOrdiniEntities(int maxResults, int firstResult) {
        return findOrdiniEntities(false, maxResults, firstResult);
    }

    private List<Ordini> findOrdiniEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Ordini.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Ordini findOrdini(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ordini.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrdiniCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Ordini> rt = cq.from(Ordini.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
